package kis.io.xml.bysocket.apply;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.StringReader;
import java.io.UnsupportedEncodingException;
import java.net.Socket;
import java.net.SocketException;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.jdom.Document;
import org.jdom.input.SAXBuilder;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;
import org.xml.sax.InputSource;

public class HandlerThread implements Runnable
{
	private Socket socket = null;

	/**
	 * @param socket
	 */
	public HandlerThread(Socket socket)
	{
		this.socket = socket;
	}

	public void run()
	{
		InputStream is = null;

		OutputStream os = null;
		PrintWriter pw = null;
		try
		{
			socket.setSoTimeout(6 * 1000);
		} catch (SocketException e1)
		{
			System.out.println("连接超时");
		}
		try
		{
			// 取得输入流
			is = socket.getInputStream();
			byte[] b = new byte[10];
			is.read(b);
			int contentSize = Integer.parseInt(new String(b, "GBK"));
			byte[] xml = new byte[contentSize];
			is.read(xml);
			System.out.println(new String(xml, "GBK"));
			//TODO 逻辑处理
			File returnXml= handle(new String(xml, "GBK"));
			socket.shutdownInput();
			os = socket.getOutputStream();
			pw = new PrintWriter(os);
			pw.write(getXML(returnXml));
			pw.flush();
		} catch (IOException e)
		{
			System.out.println("服务器异常：" + e.getMessage());
		} finally
		{
			try
			{
				if (pw != null)
				{
					pw.close();
				}
				if (os != null)
				{
					os.close();
				}
				if (is != null)
				{
					is.close();
				}
				if (socket != null)
				{
					socket.close();
				}
			} catch (IOException e)
			{
				socket = null;
				System.out.println("服务器异常：" + e.getMessage());
			}
		}
	}

	public static File handle(String xml)
	{
		String returnPath = null;
		File f = null;
		try
		{

			StringReader sr = new StringReader(xml.trim());
			Document doc = new SAXBuilder().build(new InputSource(sr));
			Format format = Format.getPrettyFormat();
			format.setEncoding("GBK");
			XMLOutputter putter = new XMLOutputter(format);
			SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
			String path = HandlerThread.class.getResource("/").toString().replace("file:", "") + "/xml/" + sdf.format(new Date()) + ".xml";
			System.out.println(path);
			putter.output(doc, new FileOutputStream(path));
			// 读取xml数据
//			Map<String, String> xmlContent = AnalyzeXml.returnXmlContent(path);
//			XmlFactory factory = Factory.createFactory(xmlContent.get("trxcode"));
//			returnPath = factory.returnBackXml(xmlContent);
			System.out.println(returnPath);
			f=new File(returnPath);
		} catch (Exception e)
		{
			// TODO: handle exception
		}
		return f;
	}

	public static String getXML(File file)
	{
		String xml = "";
		try
		{
			try
			{
				InputStreamReader read = new InputStreamReader(new FileInputStream(file), "GBK");
				BufferedReader reader = new BufferedReader(read);
				String line = "";
				try
				{
					while ((line = reader.readLine()) != null)
					{
						xml += line;
					}
				} catch (IOException e)
				{
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			} catch (UnsupportedEncodingException e)
			{
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		} catch (FileNotFoundException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println(xml);
		return xml;
	}
}
